/**
 * @param {number[]} nums
 * @return {number[]}
 */
 var findDuplicates = function(nums) {
  let i = 0;
  let ret = [];
  while(i < nums.length) {
      while(nums[i] && nums[i] !== i + 1) {
          if(nums[nums[i] - 1] === nums[i]) {
              ret.push(nums[i]);
              nums[i] = 0;
              break;
          }
          const temp = nums[nums[i] - 1];
          nums[nums[i] - 1] = nums[i];
          nums[i] = temp;
      }
      i++;
  }
  return ret;
};